import { Component, OnInit } from '@angular/core';
// 1. 引入forms中的组件
import { FormGroup, FormBuilder, Validators } from '@angular/forms';
// 2. 引入ng2-validation中的组件
import { CustomValidators } from 'ng2-validation';

import { NzMessageService } from 'ng-zorro-antd';
import { UserService } from 'src/app/core/service/user.service';
import { ActivatedRoute } from '@angular/router';

@Component({
  selector: 'app-reset-password',
  templateUrl: './reset-password.component.html',
  styleUrls: ['./reset-password.component.scss']
})
export class ResetPasswordComponent implements OnInit {
  // 表单验证
  passwordForm: FormGroup;
  data = {};
  activationId = '';

  constructor(
    private userService: UserService,
    private message: NzMessageService,
    private route: ActivatedRoute,
    fb: FormBuilder
  ) {
    this.passwordForm = fb.group({
      password: [null, Validators.required],
      passwordConfirm: [null, Validators.required],
    });
  }

  getPassword(): void {
    console.log(this.passwordForm.controls);
    // tslint:disable-next-line:forin
    for (const c in this.passwordForm.controls) {
      this.passwordForm.controls[c].markAsTouched();
    }
    if (this.passwordForm.valid) {
      this.data = {
        'activationId': this.activationId,
        'password': this.passwordForm.value.password,
        'passwordConfirm': this.passwordForm.value.passwordConfirm
      };
      this.pwdService(this.data);
    } else {
      this.message.error('表单校验失败', { nzDuration: 10000 });
    }
  }


  private pwdService(info: any) {
    this.userService.getPassword(info).subscribe((pwdResult) => {
      console.log('== pwdService.info ==', info);
      console.log('== pwdService.pwdResult ==', pwdResult);
      if (pwdResult.status === 1) {
        this.message.success('添加成功!', {
          nzDuration: 10000
        });
      }
      if (pwdResult.status === 0) {
        this.message.error('uploadResult.msg', {
          nzDuration: 10000
        });
      }
    });
  }

  ngOnInit() {
    this.activationId = this.route.snapshot.queryParams['activationId'];
    console.log(this.activationId);
  }

}
